<?php
/**
 * Created by PhpStorm.
 * User: eric
 * Date: 2019/6/24
 * Time: 12:01
 */

namespace App\Http\Repositories\Redis;


use App\Util\Helper;
use Illuminate\Support\Facades\DB;

class CacheDataRedis extends BaseRedis
{
    //类单例数组
    private static $instance = [];
    //redis连接句柄
    protected $_redis = false;
    protected $prefix = 'httzx:cache_data';//redis key前缀
    protected static $configKey = 'cache'; //默认的database内配置的连接的指定redis数据库
    protected $setCacheTime = 86400; //普通set生存时间 一天

    private function __construct($config)
    {
        $database = $config['database'];
        if(isset($config['persistent']) && $config['persistent']){
            $func = 'pconnect';
            //注意 $persistent_id 此处，持久连接设置标识
            $persistent_id = 'pconnect_' . $database;
        } else {
            $func = 'connect';
            $persistent_id = null;
        }
        $this->_redis = new \Redis;
        $this->_redis->$func($config['host'], $config['port'], 0, $persistent_id);
        if ('' != $config['password']) {
            $this->_redis->auth($config['password']);
        }
        $this->_redis->select((int)$database);
    }

    /**
     * 获取类单例
     * @return self
     */
    public static function getRedisInstance()
    {
        $config = config("database.redis." . self::$configKey);
        $database = $config['database'];
        $database = (int)$database;
        if (!isset(self::$instance[$database])) {
            self::$instance[$database] = new self($config);
        }
        return self::$instance[$database];
    }


    /**
     * 获取系统配置规则
     * 如$config_key=shop_info则获取网站信息数组
     * 如$config_key=shop_info.logo则获取网站logo字符串
     * @param string $setName
     * @return array|mixed|null
     */
    public function systemConfig($setName = '')
    {
        $keyPrefix = $this->prefix.":system_sets";
        $aSystemConfig = $this->_redis->GET($keyPrefix);
        if (empty($aSystemConfig)) {
            $aSystemConfig = $this->systemConfigCache();
        } else {
            $aSystemConfig = json_decode($aSystemConfig, true);
        }
        $param = explode('.', $setName);
        if(isset($param[1]) && !empty($param[1])) {//返回具体配置
            if($param[0] == 'words_str'){//如果是词组类型 处理出来
                return Helper::wordsParse($aSystemConfig[$param[0]][$param[1]]);
            } else {
                return $aSystemConfig[$param[0]][$param[1]];
            }
        } elseif(isset($param[0]) && !empty($param[0])) {//返回组配置
            return $aSystemConfig[$param[0]];
        } else {
            return $aSystemConfig;
        }
    }

    /**
     * @return array
     */
    public function systemConfigCache()
    {
        $systemSetDao = DB::table('system_sets')->select('id', 'type', 'name', 'val')->get();
        $aSystemSet = array();
        foreach ($systemSetDao as $config) {
            if($config->type){
                $aSystemSet [$config->type] [$config ->name] = $config->val;
            } else {//没有设置type的直接以name作为一级key
                $aSystemSet [$config->name] = $config->val;
            }
        }
        $keyPrefix = $this->prefix.":system_sets";
        $this->_redis->SETEX($keyPrefix, $this->setCacheTime * 10, json_encode($aSystemSet));
        return $aSystemSet;
    }

}
